package com.sec.foodit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
  
public class LazyAdapter extends BaseAdapter { 
  
    private Activity activity; 
    private ArrayList<Restaurant> data; 
    private static LayoutInflater inflater=null;   
  
    public LazyAdapter(Activity a, ArrayList<Restaurant> d) { 
        activity = a; 
        data=d; 
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    } 
  
    public int getCount() { 
        return data.size(); 
    } 
  
    public Object getItem(int position) { 
        return position; 
    } 
  
    public long getItemId(int position) { 
        return position; 
    } 
  
    public View getView(int position, View convertView, ViewGroup parent) { 
        View vi=convertView; 
        if(convertView==null) 
            vi = inflater.inflate(R.layout.list_row, null); 
  
        TextView title = (TextView)vi.findViewById(R.id.title); // title 
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name 
        TextView duration = (TextView)vi.findViewById(R.id.duration); // duration 
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        RatingBar point = (RatingBar)vi.findViewById(R.id.point);
  
        
        Restaurant rest = data.get(position);
        
        // Setting all values in listview 
        title.setText(rest.getName()); 
        artist.setText(rest.getIntro()); 
        duration.setText(rest.getLoc());
        point.setRating(rest.getPoint());
        thumb_image.setImageBitmap(DataManager.getInstance().getBitmapFromPath(rest.getPhoto()));
        
        vi.setTag(rest.getName());
        
        vi.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Log.e("foodit", "onClick()::Tag: " + v.getTag());
				DataManager.getInstance().curRestName = v.getTag().toString();
				activity.startActivity(new Intent("com.sec.foodit.RestActivity"));
			}
		});
        return vi; 
    } 
} 

